package com.hoioy.findjob.jihe;

import java.util.ArrayList;
import java.util.List;

public class LInkReverseTest {
    public static void main(String[] args) {
        Node a = new Node(null, "1");
        Node b = new Node(a, "2");
        Node c = new Node(b, "3");

        Node dd = c.reverse(c);
        dd.printList(dd);
    }

    public static Node nodeResult = new Node();

    public static void reverse(Node n, Node nodeList) {
        if (n != null) {
            Node append = new Node(nodeList, n.getMsg());
            reverse(n.getNext(), append);
        } else {
            nodeResult = nodeList;
        }
    }
}

class Node {
    private Node next;
    private String msg;

    public Node() {

    }

    public Node(Node next, String msg) {
        this.next = next;
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public synchronized void initToArray(Node n, ArrayList<Node> arrayList) {
        arrayList.add(n);
        if (n != null && null != n.getNext()) {
            initToArray(n.getNext(), arrayList);
        }
    }

    public void printList(Node n) {
        System.out.println(n.getMsg());
        if (n != null && null != n.getNext()) {
            printList(n.getNext());
        }
    }

    public Node reverse(Node n) {
        ArrayList<Node> arrayList = new ArrayList();
        initToArray(n, arrayList);

        for (int i = arrayList.size() - 1; i > 0; i--) {
            Node last = arrayList.get(i-1);
            Node the = arrayList.get(i-1);
            the.setNext(last);
        }
        arrayList.get(0).setNext(null);

        return arrayList.get(arrayList.size() - 1);
    }

}